/*
 * @Date: 2019-05-22 21:55:11
 * @LastEditors: SkyBlue
 * @LastEditTime: 2019-05-22 21:55:11
 */
package com.skyblue.controller;

import com.skyblue.entity.RealTime;
import com.skyblue.entity.Security;
import com.skyblue.service.DefaultService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.plugin.javascript.navig.Link;

import java.util.*;

@Controller
public class DefaultController {
    @Autowired
    private DefaultService defaultService;

    // 接受前端信息
    @RequestMapping(value = "/api/show", method = RequestMethod.POST)
    @ResponseBody
    public Object Default(@RequestParam String yuzhi) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", defaultService.queryBasic(Integer.parseInt(yuzhi)));
        return map;
    }

    @RequestMapping(value = "/api/security", method = RequestMethod.POST)
    @ResponseBody
    public Object Security(@RequestBody Map<String, String> params) {
        List<List<Security>> result = new ArrayList<>();
        List<Security> allSecurity = defaultService.queryAllSecurity();
        String sublocation = params.get("sublocation");
        List<Security> allMsg = defaultService.queryBylocationkey(sublocation);
        result.add(allMsg);
        result.add(allSecurity);
        // System.out.println(allMsg.get(0).getSecurity());
        // List<RealTimeView> data=;
        // Map<String, Object> map = new HashMap<String, Object>();
        // map.put("data", defaultService.queryBylocationkey(sublocation));
        return result;
    }

    @RequestMapping(value = "/api/modifyByLocationKey", method = RequestMethod.POST)
    @ResponseBody
    public int modifyByLocationKey(@RequestBody List<List<String>> modifyParams) {
        Security security = new Security();
        security.setLocationkey(Integer.parseInt(modifyParams.get(0).get(1)));
        security.setMark(modifyParams.get(5).get(1));
        security.setAddress(modifyParams.get(2).get(1));
        security.setLocation(modifyParams.get(1).get(1));
        security.setPhone1(modifyParams.get(3).get(1));
        security.setPhone2(modifyParams.get(4).get(1));
        int result;
        try {
            result = defaultService.modifyByLocationKey(security);
        } catch (Exception e) {
            result = -1;
        }
        return result;
    }

    @RequestMapping(value = "/api/queryBySecurity", method = RequestMethod.POST)
    @ResponseBody
    public Object queryBySecurity(@RequestBody Map<String, String> Security) {
        // System.out.println(security);
        // return ;
        return defaultService.queryBySecurity(Security.get("Security"));
    }

    // 只修改负责人信息
    @RequestMapping(value = "/api/modifyBysublocation", method = RequestMethod.POST)
    @ResponseBody
    public int modifyBysublocation(@RequestBody Map<String, String> map) {
        RealTime realView = new RealTime();
        realView.setLocationkey(Integer.parseInt(map.get("locationKey")));
        realView.setSublocation(map.get("subLocation"));
        int result;
        try {
            result = defaultService.modifyBySubLocation(realView);
        } catch (Exception e) {
            result = -1;
        }
        return result;
    }

    // 更换负责人
    @RequestMapping(value = "/api/replaceSecurity", method = RequestMethod.POST)
    @ResponseBody
    public int replaceSecurity(@RequestBody List<List<String>> modifyParams) {
        System.out.println(modifyParams);
        int result = 0;
        Integer type = Integer.parseInt(modifyParams.get(7).get(1));
        RealTime realTime = new RealTime();
        realTime.setLocationkey(Integer.parseInt(modifyParams.get(0).get(1)));
        realTime.setSublocation(modifyParams.get(6).get(1));
        // Security security = new Security();
        // security.setLocationkey(Integer.parseInt(modifyParams.get(0).get(1)));
        // security.setMark(modifyParams.get(5).get(1));
        // security.setAddress(modifyParams.get(2).get(1));
        // security.setLocation(modifyParams.get(1).get(1));
        // security.setPhone1(modifyParams.get(3).get(1));
        // security.setPhone2(modifyParams.get(4).get(1));
        System.out.println(type);
        if (type == 1) {
            result = defaultService.modifyBySubLocation(realTime);
            System.out.println("修改地方并修改信息");
            if (result == 1) {
                Security security = new Security();
                security.setLocationkey(Integer.parseInt(modifyParams.get(0).get(1)));
                security.setMark(modifyParams.get(5).get(1));
                security.setAddress(modifyParams.get(2).get(1));
                security.setLocation(modifyParams.get(1).get(1));
                security.setPhone1(modifyParams.get(3).get(1));
                security.setPhone2(modifyParams.get(4).get(1));
                result = defaultService.modifyByLocationKey(security);
            } else {
                result = -1;
            }
        } else if (type == 2) {
            System.out.println("只修改地方");
            result = defaultService.modifyBySubLocation(realTime);
        }
        return result;
    }

    @RequestMapping(value = "/api/allSecurity", method = RequestMethod.POST)
    @ResponseBody
    public Object getAllSecurity() {
        return defaultService.querySecurityMsg();
    }

    @RequestMapping(value = "/api/queryAllSubBykey", method = RequestMethod.POST)
    @ResponseBody
    public Object queryAllSubBykey(@RequestBody Map<String, Integer> locationkey) {
        List<RealTime> realTime = defaultService.queryAllSubBykey(locationkey.get("locationkey"));
        List<String> result = new ArrayList<>();
        for (RealTime item : realTime) {
            result.add(item.getSublocation());
        }
        return result;
    }

    @RequestMapping(value = "/api/addNewSublocation", method = RequestMethod.POST)
    @ResponseBody
    public int addNewSublocation(@RequestBody Map<String, String> newSublocation) {
        String Sublocation = newSublocation.get("newsublocation");
        Integer locationkey = Integer.parseInt(newSublocation.get("locationkey"));
        RealTime realTime = new RealTime();
        realTime.setLocationkey(locationkey);
        realTime.setSublocation(Sublocation);
        if (defaultService.querySub(Sublocation) > 0) {
            return 0;
        } else {
            int r = defaultService.addNewSublocation(realTime);
            if (r == 1) {
                return 1;
            } else {
                return -1;
            }
        }
    }

    @RequestMapping(value = "/api/delectSubBySub", method = RequestMethod.POST)
    @ResponseBody
    public int delectSubBySub(@RequestBody Map<String, String> sublocation) {
        System.out.println(sublocation);
        if (defaultService.delectSubBySub(sublocation.get("sublocation")) == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    @RequestMapping(value = "/api/getRealImg", method = RequestMethod.POST)
    @ResponseBody
    public String getRealImg(@RequestBody Map<String, String> sublocation) {
        System.out.println(defaultService.queryBySub(sublocation.get("sublocation")));
        return defaultService.queryBySub(sublocation.get("sublocation")).get(0).getImg();
    }

    @RequestMapping(value = "/api/addSec", method = RequestMethod.POST)
    @ResponseBody
    public int addSec(@RequestBody Map<String, String> security) {
        Security newsecurity = new Security();
        newsecurity.setAddress(security.get("address"));
        newsecurity.setPhone1(security.get("phone1"));
        newsecurity.setPhone2(security.get("phone2"));
        newsecurity.setSecurity(security.get("security"));
        newsecurity.setMark(security.get("mark"));
        newsecurity.setSecimg(security.get("secimg"));
        newsecurity.setLocation(security.get("location"));
        try {
            return defaultService.addSec(newsecurity);
        } catch (Exception e) {
            return -1;
        }
    }
}